package com.example.geange.demo01;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StartActivity extends AppCompatActivity {

    private MyDBOpenhelper dbOpenhelper = new MyDBOpenhelper(this, "wether.db", null, 1);

    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        progressBar = (ProgressBar) findViewById(R.id.progressBarstart);
        new MyAsyncTask2().execute();


    }


    //启动时加载本地数据的异步加载类
    class MyAsyncTask2 extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressBar.setVisibility(View.VISIBLE);

        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            progressBar.setVisibility(View.GONE);
            startActivity(new Intent(getApplication(), MainActivity.class));
//            StartActivity.this.finish();


        }

        @Override
        protected Void doInBackground(Void... voids) {
            List<CityWeather> listc;
            if (Weather.list.isEmpty()) {
                listc = getAllfromDB();
                if (!listc.isEmpty()){
                    for (CityWeather cweather : listc) {
                        Map<String, Object> map = new HashMap<String, Object>();
                        map.put("address", cweather.cityCode);
                        map.put("temperature", cweather.temperature);
                        map.put("weather", cweather.wetherType);
                        map.put("pic", setPicture(cweather.wetherType));
                        Log.i("click_weather", cweather.cityCode + " " + cweather.temperature + " " + cweather.wetherType);
                        Weather.list.add(map);
                    }
//                    return null;

                }
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }


            return null;
        }
    }

    /**
     * 数据库操作函数
     * sqLiteDatabase.execSQL("CREATE TABLE weather(num INTEGER PRIMARY KEY AUTOINCREMENT, citycode VARCHAR(12),
     * weatherType VARCHAR(20), temperature VARCHAR(20), freshflag  INTEGER) ");
     */

    //获取SQLite里面的全部数据
    public List<CityWeather> getAllfromDB() {
        List<CityWeather> weathers = new ArrayList<>();
        CityWeather ctweather;
        SQLiteDatabase db = dbOpenhelper.getReadableDatabase();
        Cursor cursor = db.query("weather", null, null, null, null, null,null);
        while (cursor.moveToNext()){
            Log.i("dbxys", "执行遍历");
            String citycdnum = cursor.getString(cursor.getColumnIndex("citycodenum"));
            String citycd = cursor.getString(cursor.getColumnIndex("citycode"));
            String weatherTp = cursor.getString(cursor.getColumnIndex("weatherType"));
            String temp = cursor.getString(cursor.getColumnIndex("temperature"));
            Log.i("dbxys",citycd+weatherTp+temp);
            int flag = cursor.getInt(cursor.getColumnIndex("freshflag"));
            ctweather = new CityWeather(citycdnum, citycd, weatherTp, temp);
            weathers.add(ctweather);
        }
        cursor.close();
        return weathers;
    }

    //根据天气类型来选泽天气图片
    public static int setPicture(String type) {
        int img = 0;
        switch (type) {
            case "晴": {
                img = R.drawable.sunny;
                break;
            }
            case "阴": {
                img = R.drawable.overcast;
                break;
            }
            case "多云": {
                img = R.drawable.cloudy;
                break;
            }
            case "少云": {
                img = R.drawable.cloudy;
                break;
            }
            case "小雨": {
                img = R.drawable.smallrain;
                break;
            }
            case "中雨": {
                img = R.drawable.heavyrain;
                break;
            }
            case "大雨": {
                img = R.drawable.heavyrain;
                break;
            }
            case "阵雨": {
                img = R.drawable.rain;
                break;
            }
            case "雷阵雨": {
                img = R.drawable.rain;
                break;
            }
            case "暴雨": {
                img = R.drawable.rain;
                //map.put("pic", );
                break;
            }
            case "雾": {
                img = R.drawable.fog;
                break;
            }
            case "霾": {
                img = R.drawable.fog;
                break;
            }
            case "霜冻": {
                //map.put("pic", );
                break;
            }
            case "暴风": {
                //map.put("pic", );
                break;
            }
            case "台风": {
                //map.put("pic", );
                break;
            }
            case "暴风雪": {
                //map.put("pic", );
                break;
            }
            case "大雪": {
                img = R.drawable.snow;
                break;
            }
            case "中雪": {
                img = R.drawable.snow;
                break;
            }
            case "小雪": {
                img = R.drawable.snow;
                break;
            }
            case "雨夹雪": {
                img = R.drawable.snow;
                break;
            }
            case "冰雹": {
                break;
            }
            case "浮尘": {
                break;
            }
            case "扬沙": {
                break;
            }
        }
        return img;
    }

    @Override
    protected void onStop() {
        super.onStop();
        StartActivity.this.finish();
    }

}
